package edu.boun.g4.coursity.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.jsoup.Jsoup;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.boun.g4.coursity.domain.Course;
import edu.boun.g4.coursity.domain.Institution;
import edu.boun.g4.coursity.dto.CourseDto;
import edu.boun.g4.coursity.dto.InstitutionDto;
import edu.boun.g4.coursity.service.CommentService;
import edu.boun.g4.coursity.service.CourseService;
import edu.boun.g4.coursity.service.InitiativeService;
import edu.boun.g4.coursity.service.InstitutionService;
import edu.boun.g4.coursity.service.InstructorService;
import edu.boun.g4.coursity.service.LanguageService;
import edu.boun.g4.coursity.service.SemanticTagService;

/**
 * @author mertusta
 * @author bulutkorkmaz
 * 
 *         Handles and retrieves institution request
 */
@Controller
@RequestMapping("/institution")
public class InstitutionController {

 
	@Resource(name = "courseService")
	private CourseService courseService;

	@Resource(name = "initiativeService")
	private InitiativeService initiativeService;

	@Resource(name = "institutionService")
	private InstitutionService institutionService;

	@Resource(name = "instructorService")
	private InstructorService instructorService;

	@Resource(name = "languageService")
	private LanguageService languageService;

	@Resource(name = "commentService")
	private CommentService commentService;

	@Resource(name = "semanticTagService")
	private SemanticTagService semanticTagService;

	@RequestMapping(method = RequestMethod.POST, value = "/add")
	public @ResponseBody
	Response addInstitution(@RequestParam String name, @RequestParam String info) {

		Institution institution = new Institution();
		institution.setInstitutionName(name);
		institution.setInfo(info);

		Response response = new Response();
		response.setBody(institution);
		response.setStatus(true);

		institutionService.add(institution);

		return response;
	}

	@RequestMapping(method = RequestMethod.POST, value = "/delete")
	public @ResponseBody
	Response deleteInstitution(@RequestParam Integer id) {

		Response response = new Response();
		response.setBody(null);
		response.setStatus(true);

		institutionService.delete(id);

		return response;
	}

	@RequestMapping(method = RequestMethod.POST, value = "/edit")
	public @ResponseBody
	Response editInstitution(@RequestParam String name,
			@RequestParam String info) {

		Institution ExistingInstitution = new Institution();
		ExistingInstitution.setInstitutionName(name);
		ExistingInstitution.setInfo(info);

		Response response = new Response();
		response.setBody(null);
		response.setStatus(true);

		institutionService.edit(ExistingInstitution);

		return response;
	}

	@RequestMapping(value = "/list")
	public @ResponseBody
	Response listInstitution() {

		List<Institution> all = institutionService.getAll();

		Response response = new Response();
		
		response.setStatus(true);
		
		List<InstitutionDto> dtos = new ArrayList<InstitutionDto>();

		InstitutionDto dto;
		for (Institution institution : all) {
			dto = new InstitutionDto(institution.getId(),institution.getInstitutionName(),institution.getInfo());
			dtos.add(dto);
		}
		response.setBody(dtos);
		return response;
	}

	@RequestMapping(method = RequestMethod.POST, value = "/listCourses")
	public @ResponseBody
	Response listCourses(@RequestParam int id) {

		List<Course> all = courseService.getByInstitutionId(id);
		List<CourseDto> dtos = new ArrayList<CourseDto>();

		CourseDto dto;
		for (Course course : all) {
			dto = new CourseDto();
			dto.setCategories(semanticTagService.getAllCategories(course
					.getCourseId()));
			dto.setCertificate(course.getCertificate());
			dto.setComments(commentService.getAll(course.getCourseId()));
			dto.setCountry(course.getCountry());
			dto.setCourseDescription(Jsoup.parse(course.getCourseDescription()).text().substring(0, 100)+"...");
			dto.setCourseId(course.getCourseId());
			dto.setCourseName(course.getCourseName());
			dto.setEstimatedEffort(course.getEstimatedEffort());
			dto.setExam(course.isExam());
			dto.setForum(course.isForum());
			dto.setInfo(course.getInfo());
			dto.setInitiative(initiativeService.getAll(course.getCourseId()));
			dto.setInstitution(institutionService.getAll(course.getCourseId()));
			dto.setInstructor(instructorService.getAll(course.getCourseId()));
			dto.setLanguages(course.getLanguages());
			dto.setLength(course.getLength());
			dto.setPeerAssesment(course.isPeerAssesment());
			dto.setPrerequisites(courseService.getAllPrerequisite(course
					.getCourseId()));
			dto.setRating(course.getRating());
			dto.setRecommendedBackground(course.getRecommendedBackground());
			dto.setStartingDate(course.getStartingDate());
			dto.setTeamProjects(course.isTeamProjects());
			dto.setTextbook(course.getTextbook());
			dto.setUrl(course.getUrl());
			dto.setVideoLectures(course.isVideoLectures());

			dtos.add(dto);
		}

		Response response = new Response();
		response.setBody(dtos);
		response.setStatus(true);

		return response;
	}

}